package com.wrial.offer;
/*
 * @Author  Wrial
 * @Date Created in 20:50 2020/3/31
 * @Description 从尾打印倒头
 */

public class FaceTest_06 {
    public int[] reversePrint(ListNode head) {

        if (head == null) return new int[]{};
        int count = 0;
        ListNode cur = head;
        // 统计个数
        while (cur != null) {
            count += 1;
            cur = cur.next;
        }
        cur = head;
        int[] ans = new int[count];
        // 使用倒序插入节省效率
        for (int i = count - 1; i >= 0; i--) {
            ans[i] = cur.val;
            cur = cur.next;
        }
        return ans;

    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
}
